package com.example.jdk8.time;


import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class java8Test9 {
    public static void main(String[] args) {

//. 解析和格式化：将一个日期格式转换为不同的格式，之后再解析一个字符串，得到日期时间对象，这些都是很常见的。我们来看一下简单的例子。
        //Format examples
        LocalDate date = LocalDate.now();
        //default format
        System.out.println("Default format of LocalDate="+date);
        //specific format
        System.out.println(date.format(DateTimeFormatter.ofPattern("d::MMM::uuuu")));
        System.out.println(date.format(DateTimeFormatter.ofPattern("d::MM::uuuu")));
        System.out.println(date.format(DateTimeFormatter.BASIC_ISO_DATE));

        LocalDateTime dateTime = LocalDateTime.now();
        //default format
        System.out.println("Default format of LocalDateTime="+dateTime);
        //specific format
        System.out.println(dateTime.format(DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss")));
        System.out.println(dateTime.format(DateTimeFormatter.ofPattern("d::MM::uuuu HH::mm::ss")));
        System.out.println(dateTime.format(DateTimeFormatter.BASIC_ISO_DATE));

        Instant timestamp = Instant.now();
        //default format
        System.out.println("Default format of Instant="+timestamp);

        //Parse examples
//        LocalDateTime dt = LocalDateTime.parse("27::4月::2014 21::39::48", DateTimeFormatter.ofPattern("d::MM::uuuu HH::mm::ss"));
        LocalDateTime dt2 = LocalDateTime.parse("27::04::2014 21::39::48", DateTimeFormatter.ofPattern("d::MM::uuuu HH::mm::ss"));

//        System.out.println("Default format after parsing = "+dt);
        System.out.println("Default format after parsing = "+dt2);
//        Default format of LocalDate=2014-04-28
//        28::Apr::2014
//        20140428
//        Default format of LocalDateTime=2014-04-28T16:25:49.341
//        28::Apr::2014 16::25::49
//        20140428
//        Default format of Instant=2014-04-28T23:25:49.342Z
//        Default format after parsing = 2014-04-27T21:39:48


        LocalDateTime arrivalDate = LocalDateTime.now();
        try {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("MM-dd-yyyy hh:mm a");
            String landing = arrivalDate.format(format);
            System.out.printf("Arriving at : %s %n", landing);
        } catch (DateTimeException ex) {
            System.out.printf("%s can't be formatted!%n", arrivalDate);
            ex.printStackTrace();
        }



    }
}
